/*
 * Copyright (C) 2008 Peter Kling
 * 
 * This file is part of 'A Kind of Billiard', a graphical billiard simulation.
 * 
 * 'A Kind of Billiard' is free software: you can redistribute it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * 'A Kind of Billiard' is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 'A Kind of Billiard'. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package de.akob.billiard;

import javax.media.opengl.GL;

import com.sun.opengl.util.GLUT;

import de.akob.misc.OpenGLDrawable;
import de.akob.modeling.GLModel;
import de.akob.physics.PhysicalObject;

/**
 * A billiard ball. This class extends a physical ball by the possibility to draw it using OpenGL. Custom models can be
 * loaded using the <code>setModel</code> method.
 */
public class BilliardBall extends PhysicalObject implements OpenGLDrawable {
	private static final int GLUTSPHERE_SLICES = 25;
	private static final int GLUTSPHERE_STACKS = 25;
	private GLModel          model;
	private GLUT             glut;
	
	/**
	 * Constructs a billiard ball of the specified mass and radius. See class <code>Billiard</code> for predefined
	 * constants.
	 * 
	 * @param mass mass of the billiard ball
	 * @param radius radius of the billiard ball
	 * @see Billiard
	 */
	public BilliardBall(double mass, double radius) {
		super(mass, radius);
		glut = new GLUT();
	}
	
	/**
	 * OpenGL method to draw the billiard ball.
	 * 
	 * @param gl OpenGL interface
	 */
	public void draw(GL gl) {
		gl.glPushMatrix();
		gl.glTranslated(position.get(1), position.get(2), position.get(3));
		if (model != null) {
			model.draw(gl);
		} else {
			glut.glutSolidSphere(radius, GLUTSPHERE_SLICES, GLUTSPHERE_STACKS);
		}
		gl.glPopMatrix();
	}
	
	/**
	 * Sets the model for the ball. If no model is specified (that is if model is null), a ball using the current OpenGL
	 * color is drawn.
	 * 
	 * @param model model used to draw the billiard ball
	 */
	public void setModel(GLModel model) {
		this.model = model;
	}
}
